package com.wanlink.mall.base;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanlink.mall.R;

import butterknife.ButterKnife;

/**
 *
 * user：chenqm
 * desc：BaseActivity
 * date：2018/10/10 14:12
 *
 */

public abstract class BaseActivity<V, T extends BasePresenter<V>> extends BActivity {

    protected T mPresenter;

    RelativeLayout layoutTitleBar;
    //左边view
    RelativeLayout leftContent;
    //返回按钮
    ImageView ivBack;
    //左边文字按钮
    TextView tvLeft;
    //中间标题
    public TextView centetTitle;
    //右边view1
    RelativeLayout rightContent1;
    //右边图标按钮1
    ImageView ivRight1;
    //右边文字按钮1
    TextView tvRight1;

    //右边view2
    RelativeLayout rightContent2;
    //右边图标按钮2
    ImageView ivRight2;
    //右边文字按钮2
    TextView tvRight2;

    public FrameLayout rootContent;



    @Override
    public View findViewById(int id) {
        return rootContent.findViewById(id);
    }

    private void clearContentView() {
        rootContent.removeAllViews();
    }

    @Override
    public void setContentView(int layoutResID) {
        clearContentView();
        View view = getLayoutInflater().inflate(layoutResID, rootContent, true);
        ButterKnife.bind(this, view);
    }

    @Override
    public void setContentView(View view) {
        clearContentView();
        rootContent.addView(view);
        ButterKnife.bind(this, view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        clearContentView();
        rootContent.addView(view, params);
        ButterKnife.bind(this, view);
    }


    @Override
    public void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroy();
    }

    //在setContentView()调用之前调用，可以设置WindowFeature(如：this.requestWindowFeature(Window.FEATURE_NO_TITLE);)
    @Override
    public void init() {
        super.init();
        //判断是否使用MVP模式
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
        }

        getDelegate().setContentView(R.layout.activity_base);
        layoutTitleBar = getWindow().findViewById(R.id.layout_title_bar);
        if (!needTitleBar()){
            layoutTitleBar.setVisibility(View.GONE);
        }
        leftContent = layoutTitleBar.findViewById(R.id.left_con);
        ivBack = layoutTitleBar.findViewById(R.id.iv_back);
        tvLeft = layoutTitleBar.findViewById(R.id.tv_left);

        centetTitle = layoutTitleBar.findViewById(R.id.center_title);

        rightContent1 = layoutTitleBar.findViewById(R.id.right_con1);
        tvRight1 = layoutTitleBar.findViewById(R.id.tv_right1);
        ivRight1 = layoutTitleBar.findViewById(R.id.iv_right1);

        rightContent2 = layoutTitleBar.findViewById(R.id.right_con2);
        tvRight2 = layoutTitleBar.findViewById(R.id.tv_right2);
        ivRight2 = layoutTitleBar.findViewById(R.id.iv_right2);

        rootContent = getWindow().findViewById(R.id.content);

        setTitleBack(true);
    }

    //用于创建Presenter和判断是否使用MVP模式(由子类实现)
    protected abstract T createPresenter();


    /**
     * 是否显示标题栏
     */
    public boolean needTitleBar(){
        return true;
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title) {
        if (centetTitle != null) {
            centetTitle.setVisibility(View.VISIBLE);
            centetTitle.setText(title);
        }
    }

    /**
     * 设置返回按钮是否可见
     * @param visible
     */
    public void setTitleBack(boolean visible) {
        if (ivBack != null) {
            if(visible){
                ivBack.setVisibility(View.VISIBLE);
                if(leftContent!=null){
                    leftContent.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(ivBack.isShown()){
                                finish();
                            }

                        }
                    });
                }
            }else{
                ivBack.setVisibility(View.GONE);
            }

        }
    }

    /**
     * 设置返回按钮监听
     * @param clickListener 建监听器
     */
    public void setTitleBackListener(View.OnClickListener clickListener){
        if (clickListener != null){
            leftContent.setOnClickListener(clickListener);
        }
    }

    /**
     * 设置左侧文本按钮
     * @param text
     */
    public void setTitleLeftText(String text) {
        if (tvLeft != null) {
            tvLeft.setVisibility(View.VISIBLE);
            tvLeft.setText(text);
            if(ivBack!=null){
                ivBack.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 设置左侧文本按钮和点击事件
     * @param text
     * @param listener
     */
    public void setTitleLeftText(String text, View.OnClickListener listener) {
        if (tvLeft != null) {
            tvLeft.setVisibility(View.VISIBLE);
            tvLeft.setText(text);
            if(leftContent!=null&&listener!=null){
                leftContent.setOnClickListener(listener);
            }
            if(ivBack!=null){
                ivBack.setVisibility(View.GONE);
            }
        }
    }
    /**
     * 设置右侧图标按钮1
     * @param iconId
     */
    public void setTitleRightIcon1(int iconId) {
        if (ivRight1 != null) {
            ivRight1.setVisibility(View.VISIBLE);
            ivRight1.setImageResource(iconId);
        }
    }
    /**
     * 设置右侧图标按钮1和点击事件
     * @param iconId
     * @param listener
     */
    public void setTitleRightIcon1(int iconId, View.OnClickListener listener) {
        if (ivRight1 != null) {
            ivRight1.setVisibility(View.VISIBLE);
            ivRight1.setImageResource(iconId);
            if(rightContent1!=null&&listener!=null){
                rightContent1.setOnClickListener(listener);
            }
        }
    }
    /**
     * 设置右侧图标按钮2
     * @param iconId
     */
    public void setTitleRightIcon2(int iconId) {
        if (ivRight2 != null) {
            ivRight2.setVisibility(View.VISIBLE);
            ivRight2.setImageResource(iconId);
        }
    }

    /**
     * 设置右侧图标按钮2和点击事件
     * @param iconId
     * @param listener
     */
    public void setTitleRightIcon2(int iconId, View.OnClickListener listener) {
        if (ivRight2 != null) {
            ivRight2.setVisibility(View.VISIBLE);
            ivRight2.setImageResource(iconId);
            if(rightContent2!=null&&listener!=null){
                rightContent2.setOnClickListener(listener);
            }
        }
    }

    /**
     * 设置右侧文本按钮1
     * @param text
     */
    public void setTitleRightText1(String text) {
        if (tvRight1 != null) {
            tvRight1.setVisibility(View.VISIBLE);
            tvRight1.setText(text);
        }
    }

    /**
     * 设置右侧文本按钮1和点击事件
     * @param text
     * @param listener
     */
    public void setTitleRightText1(String text, View.OnClickListener listener) {
        if (tvRight1 != null) {
            tvRight1.setVisibility(View.VISIBLE);
            tvRight1.setText(text);
            if(rightContent1!=null&&listener!=null){
                rightContent1.setOnClickListener(listener);
            }
        }
    }
    /**
     * 设置右侧文本按钮2
     * @param text
     */
    public void setTitleRightText2(String text) {
        if (tvRight2 != null) {
            tvRight2.setVisibility(View.VISIBLE);
            tvRight2.setText(text);
        }
    }

    /**
     * 设置右侧文本按钮2和点击事件
     * @param text
     * @param listener
     */
    public void setTitleRightText2(String text, View.OnClickListener listener) {
        if (tvRight2 != null) {
            tvRight2.setVisibility(View.VISIBLE);
            tvRight2.setText(text);
            if(rightContent2!=null&&listener!=null){
                rightContent2.setOnClickListener(listener);
            }
        }
    }
}
